package com.shchuangzhi.smartguard.core.command;

import com.google.gson.Gson;
import com.shchuangzhi.smartguard.SharedPref;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.Machine;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by j_hu on 11/22/17.
 */

public class ImageUpdateCommand extends Command implements Command.CommandHandler  {
    private int no;
    private String imageUrl;

    public ImageUpdateCommand(int no, String imageUrl) {
        super(Consts.COMMAND_REQ_IMAGE_UPDATE);
        this.no = no;
        this.imageUrl = imageUrl;
    }

    @Override
    protected OutputStream fill(OutputStream output) {
        DataOutputStream out = new DataOutputStream(output);
        try {
            CommandRQ<ImageUpdateCommandRQ> rq = new CommandRQ<>();
            rq.setToken(getToken());
            rq.setData(new ImageUpdateCommandRQ(no, imageUrl));
            byte[] p = new Gson().toJson(rq).getBytes();
            if (p != null && p.length > 0) {
                out.write(p);
            }
        } catch(IOException e) {
            raiseError(e);
        }
        return out;
    }

    @Override
    protected String getType() {
        return "application/json";
    }

    @Override
    protected CommandHandler getHandler(String commandName) {
        CommandHandler handler = null;
        if (commandName != null) {
            handler = commandName.equals(Consts.COMMAND_RESP_IMAGE_UPDATE) ? this : null;
        }
        return handler;
    }

    @Override
    public boolean handle(InputStream input) {
        return true;
    }

    public class ImageUpdateCommandRQ {
        private int no;
        private String url;
        public ImageUpdateCommandRQ(int no, String url) {
            this.no = no;
            this.url = url;
        }
    }
}
